/*******************************************************************************
* Copyright (c) 2010, 2018 西安秦晔信息科技有限公司
* Licensed under the Apache License, Version 2.0 (the "License");
*    you may not use this file except in compliance with the License.
*    You may obtain a copy of the License at
*
*       http://www.apache.org/licenses/LICENSE-2.0
*
*    Unless required by applicable law or agreed to in writing, software
*    distributed under the License is distributed on an "AS IS" BASIS,
*    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*    See the License for the specific language governing permissions and
*    limitations under the License.
*******************************************************************************/
package com.qinyeit.serviceapp.service.materials.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.qinyeit.serviceapp.entity.Material;
import com.qinyeit.serviceapp.exception.BusinessRunTimeException;
import com.qinyeit.serviceapp.utils.Lang;
import com.qinyeit.serviceapp.utils.SearchUtils;
import com.qinyetech.springstage.core.entity.search.SearchFilterHelper;
import com.qinyetech.springstage.core.entity.search.SearchOperator;
import com.qinyetech.springstage.core.entity.search.Searchable;
import com.qinyetech.springstage.core.lang.QyMap;
import com.qinyetech.springstage.core.repository.jdbc.PageNamedParameterJdbcTemplate;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.qinyetech.springstage.core.service.BaseTreeableService;
import com.qinyeit.serviceapp.entity.MaterialCategory;
import com.qinyeit.serviceapp.repository.materials.MaterialCategoryRepository;
import com.qinyeit.serviceapp.service.materials.MaterialCategoryService;

import java.util.List;
import java.util.Map;

/** 
 * ClassName: MaterialCategoryServiceImpl <br/> 
 * Function: 物品类别 业务实现 <br/> 
 * date: 2018-11-07 11:40:01 <br/> 
 * 
 * @author liuwenjin
 * @version 1.0
 * @since JDK 1.8
 */
@Service
@Transactional
public class MaterialCategoryServiceImpl extends BaseTreeableService<MaterialCategory, Long> implements MaterialCategoryService {


	@Autowired
	private MaterialCategoryRepository materialCategoryRepository;
	@Autowired
	private PageNamedParameterJdbcTemplate jdbcTemplate;

	@Override
    public MaterialCategory buildRoot() {
        MaterialCategory materialCategory = new MaterialCategory();
        materialCategory.setId(0L);
        materialCategory.setParentId(0L);
        materialCategory.setParentIds("");
        return materialCategory;
    }

    @Override
    public List<MaterialCategory> getBuildTree(Long merchantGroupId,Long storeId){
        Searchable searchable=Searchable.newSearchable();
        searchable.addSort(new Sort(Sort.Direction.ASC, "name"));
        searchable.addSearchParam("deleted_eq",false);
        searchable.addSearchParam("merchantGroupId_eq", merchantGroupId);
        if(storeId!=null){
            searchable.addSearchFilter(SearchFilterHelper.or(
                    SearchFilterHelper.newCondition("storeId", SearchOperator.eq,storeId),
                    SearchFilterHelper.newCondition("storeId",SearchOperator.isNull,null)
            ));
        }

        List<MaterialCategory> models = findWithSort(searchable);//查询所有
        return associateList(models, 0l);
    }
    @Override
    public List<MaterialCategory> getBuildTree(Long merchantGroupId,Long storeId,boolean isDishesCategory){
        Searchable searchable=Searchable.newSearchable();
//        searchable.addSort(new Sort(Sort.Direction.ASC, "weight"));
        searchable.addSort(new Sort(Sort.Direction.ASC, "name"));
        searchable.addSearchParam("deleted_eq",false);
        searchable.addSearchParam("merchantGroupId_eq", merchantGroupId);
        searchable.addSearchFilter(SearchFilterHelper.or(
                SearchFilterHelper.newCondition("storeId", SearchOperator.eq,storeId),
                SearchFilterHelper.newCondition("storeId",SearchOperator.isNull,null)
        ));
        searchable.addSearchFilter("dishesCategoryId",SearchOperator.isNull,null);
        List<MaterialCategory> models = findWithSort(searchable);//查询所有
        return associateList(models, 0l);
    }
    @Override
    public List<MaterialCategory> getBuildTree(Long merchantGroupId){
        Searchable searchable=Searchable.newSearchable();
        searchable.addSort(new Sort(Sort.Direction.ASC, "name"));
        searchable.addSearchParam("deleted_eq",false);
        searchable.addSearchParam("merchantGroupId_eq", merchantGroupId);
        List<MaterialCategory> models = findWithSort(searchable);//查询所有
        return associateList(models, 0l);
    }

    @Override
    public List<MaterialCategory> getBuildTree(Long merchantGroupId,Boolean deleted){
        Searchable searchable=Searchable.newSearchable();
        searchable.addSort(new Sort(Sort.Direction.ASC, "name"));
        if(deleted){
            searchable.addSearchParam("deleted_eq",false);
        }
        searchable.addSearchParam("merchantGroupId_eq", merchantGroupId);
        List<MaterialCategory> models = findWithSort(searchable);//查询所有
        return associateList(models, 0l);
    }


    @Override
    public String getNextNo(String defNo,Long merchantGroupId) {
        String sql = "SELECT no FROM material_categorys WHERE merchant_group_id=:merchantGroupId and is_deleted=false ORDER BY id DESC LIMIT 1";
        QyMap param = QyMap.map("merchantGroupId",merchantGroupId);
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql,param);
        if(CollectionUtils.isNotEmpty(maps)){
            Map<String, Object> map = maps.get(0);
            if(map.containsKey("no")){
                String lastNo = map.get("no").toString();
                return Lang.getNextNo(lastNo);
            }
        }
        return defNo;
    }



    /**
     *
     * associateList:处理关联 <br>
     *
     * @author wuqing
     * @param models
     * 返回根节点集合
     * @since JDK 1.7
     */
    private List<MaterialCategory> associateList(List<MaterialCategory> models, Long id){
        List<MaterialCategory> result= Lists.newArrayList();
        if(models!=null && models.size()>0){
            for (int i=0;i<models.size();i++) {
                MaterialCategory model = models.get(i);
                if(id.equals(model.getParentId())){
                    model.setChildren(associateList(models, (Long) model.getId()));
                    result.add(model);
                }
            }
        }
        return result;
    }

}